<template>
	<view class="countCard">
		<view class="count">
			<uni-forms ref="baseForm" :modelValue="dataSource">
				<uni-forms-item label="基础代谢:">
					<uni-easyinput type="number" v-model="dataSource.basalMetabolism" placeholder="请输入基础代谢" />
				</uni-forms-item>
				<uni-forms-item label="训练强度:">
					<uni-data-checkbox v-model="dataSource.trainingIntensity" :localdata="trainingIntensity" />
				</uni-forms-item>
				<uni-forms-item label="训练时常(分钟):" v-if="dataSource.trainingIntensity">
					<uni-easyinput type="number" v-model="dataSource.trainingDuration" placeholder="请输入训练时常" />
				</uni-forms-item>
				<uni-forms-item label="营养比例:">
					<uni-data-checkbox v-model="dataSource.nutritiveProportion" :localdata="nutritiveProportion" />
				</uni-forms-item>
			</uni-forms>
		</view>
		<view class="countBtn" @click="getNutrition">
			<text class="countText">计算</text>
		</view>
		<view class="result">
			<text class="resultText">每天摄入碳水为(g):</text>
			<uni-easyinput class="resultText" v-model="nutritive.carbohydrate" />
		</view>
		<view class="result">
			<text class="resultText">每天摄入蛋白质为(g):</text>
			<uni-easyinput class="resultText" v-model="nutritive.protein" />
		</view>
		<view class="result">
			<text class="resultText">每天摄入脂肪为(g):</text>
			<uni-easyinput class="resultText" v-model="nutritive.fat" />
		</view>
		<view class="describe">
			<text>营养比例:</text>
			<text>532:"碳水50%,蛋白质30%，脂肪20%(适用普通人)"</text>
			<text>442:"碳水40%,蛋白质40%，脂肪20%(适用碳水敏感)"</text>
		</view>
	</view>
</template>

<script setup>
	import {
		reactive,
		nextTick,
		getCurrentInstance,
		ref
	} from 'vue'
	const {
		proxy
	} = getCurrentInstance()
	const userInfo = ref(uni.getStorageSync('userInfo'))
	// 基础信息
	const dataSource = reactive({
		basalMetabolism: '',
		trainingIntensity: 0,
		trainingDuration: '',
		nutritiveProportion: 1
	})
	// 总热量
	const calories = ref()
	// 健身类型
	const trainingIntensity = ref([{
		text: '无',
		value: 0
	}, {
		text: '新手或(女生)',
		value: 1
	}, {
		text: '健身爱好者',
		value: 2
	}, {
		text: '训练强度大',
		value: 3
	}])
	// 饮食类型
	const nutritiveProportion = ref([{
		text: '532',
		value: 1
	}, {
		text: '442',
		value: 2
	}])
	// 推荐食用营养元素计量
	const nutritive = reactive({
		carbohydrate: '',
		protein: '',
		fat: ''
	})
	// 计算推荐食用营养元素计量
	const getNutrition = () => {
		if (dataSource.trainingIntensity) {
			// 计算总的代谢量
			switch (dataSource.trainingIntensity) {
				case 1:
					calories.value = Number(dataSource.basalMetabolism) + 5 * dataSource.trainingDuration
					break;
				case 2:
					calories.value = Number(dataSource.basalMetabolism) + 8 * dataSource.trainingDuration
					break
				case 3:
					calories.value = Number(dataSource.basalMetabolism) + 10 * dataSource.trainingDuration
					break
				default:
					break;
			}
		} else {
			calories.value = dataSource.basalMetabolism
		}
		// 根据饮食类别获取不同的营养元素摄入比例
		if (dataSource.nutritiveProportion == 1) {
			nutritive.carbohydrate = ((calories.value * 0.5) / 4).toFixed(0)
			nutritive.protein = ((calories.value * 0.3) / 4).toFixed(0)
			nutritive.fat = ((calories.value * 0.2) / 9).toFixed(0)
		} else if (dataSource.nutritiveProportion == 2) {
			nutritive.carbohydrate = ((calories.value * 0.4) / 4).toFixed(0)
			nutritive.protein = ((calories.value * 0.4) / 4).toFixed(0)
			nutritive.fat = ((calories.value * 0.2) / 9).toFixed(0)
		}
		setTimeout(() => {
			uni.showModal({
				title: '提示',
				content: "是否将当前营养搭配应用到您到饮食计划中?",
				success: async (res) => {
					if (res.confirm) {
						uni.showLoading({
							title: '提交中'
						})
						proxy.$http("setUserInfo", {
							carbohydrate: nutritive.carbohydrate,
							protein: nutritive.protein,
							fat: nutritive.fat
						}).then(result => {
							if (!result.errCode) {
								userInfo.value.carbohydrate = nutritive.carbohydrate
								userInfo.value.protein = nutritive.protein
								userInfo.value.fat = nutritive.fat
								uni.setStorageSync('userInfo', userInfo.value)
								uni.showToast({
									title: "应用成功",
									icon: 'none',
									duration: 1000
								})
								uni.hideLoading()
							}
						})
					} else if (res.cancel) {
						uni.showToast({
							title: "已取消",
							icon: 'none',
							duration: 1000
						})
					}
				}
			})
		}, 1000)
	}
</script>

<style lang="scss">
	.countCard {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;

		.countBtn {
			width: 160rpx;
			height: 70rpx;
			line-height: 70rpx;
			border-radius: 40rpx;
			background-color: #87C38F;
			text-align: center;
			text-indent: 20rpx;
			letter-spacing: 20rpx;
			margin: 20rpx 0;

			.countText {
				font-size: 34rpx;
				color: #fbffee;
				font-weight: bold;
			}
		}

		.result {
			display: flex;
			width: 600rpx;
			margin: 20rpx;
			font-size: 28rpx;
			color: #606266;
			align-items: center;
			justify-content: space-around;

			.resultText {
				padding: 0 10rpx;
			}
		}
	}

	.count {
		display: flex;
		justify-content: center;
		margin: 20rpx;
		margin-top: 80rpx;

		.uni-forms {
			width: 500rpx;

			.uni-forms-item__label {
				width: 160rpx !important;
			}
		}

		.uni-forms-item__content {
			display: flex;
			align-items: center;
		}
	}

	.describe {
		display: flex;
		flex-direction: column;
		margin: 20rpx;
		font-size: 24rpx;
		color: #909399;
	}
</style>